/**
 * @(#)ApkTaskQueryServiceImpl.java 2017年2月3日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.domian.channel.TasktimeDto;
import org.jsbd.boss.service.ApkManager;
import org.jsbd.boss.service.IApkTaskQueryService;
import org.jsbd.boss.service.INewTaskService;
import org.jsbd.boss.service.IRetentTaskService;
import org.jsbd.boss.util.BigDecimalUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO 请加入本类的说明
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class ApkTaskQueryServiceImpl implements IApkTaskQueryService {
	public static final Logger LOGGER = LoggerFactory.getLogger(ApkTaskQueryServiceImpl.class);

	@Resource(name = "newTaskService")
	private INewTaskService newTaskService;

	@Resource(name = "retentTaskService")
	private IRetentTaskService retentTaskService;
	@Resource
	private ApkManager apkManager;



	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IApkConfigService#queryApkConfigList(java.util.
	 * List)
	 */
	@Override
	public List<ChannelApkConfig> queryApkConfigList(List<ChannelApkConfig> apkList) {
		try {
			List<String> marks = new ArrayList<String>();
			if (apkList != null && apkList.size() > 0) {
				for (ChannelApkConfig config : apkList) {
					marks.add(config.getMark());
				}
			}
			String[] tasks = newTaskService.hasDoNum(marks);
			String[] retents = retentTaskService.hasDoNum(marks);
			for (int i = 0; i < apkList.size(); i++) {
				ChannelApkConfig config = apkList.get(i);
				String mark = config.getMark();
				String taskNum = tasks[i];
				String retNum = retents[i];
				Integer retentTotal = apkManager.getRetentTaskApkNum(mark);
				if (retentTotal == null) {
					retentTotal = 0;
				}
				config.setNewTaskNum(config.getDaylimit() + "|" + taskNum);
				config.setRetentTaskNum(retentTotal + "|" + retNum);
			}
		} catch (Exception e) {
			LOGGER.error("查询出错！", e);
		}
		return apkList;
	}

	@Override
	public Map<String, Object> queryTasktimeByBaseList(List<TasktimeDto> list) {
		List<String> marks = new ArrayList<String>();
		if (list != null && list.size() > 0) {
			for (TasktimeDto config : list) {
				marks.add(config.getMark());
			}
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> map_temp = new HashMap<String, String>();
		int newTasktimeTotal = 0;
		int retentTaskTimeTotal = 0;
		
		String[] tasks = newTaskService.hasDoNum(marks);
		String[] retents = retentTaskService.hasDoNum(marks);
		
		try {
			for (int i = 0; i < list.size(); i++) {
				TasktimeDto ttDto = list.get(i);
				String mark = ttDto.getMark();
				
				// 执行mark对应的新增任务平均时间
				Integer newTaskTime = 0;// apkManager.getApkNewTaskTime(mark);
				Integer newTaskExeNum =0 ;// Integer.valueOf(tasks[i].split("\\|")[1]);

				// 执行mark对应的留存任务平均时间
				Integer retentTaskTime = 0;// apkManager.getApkRetentTaskTime(mark);
				Integer retentExeNum = Integer.valueOf(retents[i].split("\\|")[1]);

				if (newTaskTime == null) {
					newTaskTime = 0;
				}
				if (retentTaskTime == null) {
					retentTaskTime = 0;
				}

				newTaskTime = newTaskTime * newTaskExeNum;
				retentTaskTime = retentTaskTime * retentExeNum;

				ttDto.setNewTaskTime(newTaskTime);
				ttDto.setRetentTaskTime(retentTaskTime);

				String baseKey = "base_" + ttDto.getBase();
				if (map_temp.containsKey(baseKey)) {
					String value = map_temp.get(baseKey);
					String[] value_arr = value.split("_");
					Integer newTaskTimeTemp = Integer.valueOf(value_arr[0]).intValue() + newTaskTime.intValue();
					Integer retentTaskTimeTemp = Integer.valueOf(value_arr[1]).intValue() + retentTaskTime.intValue();
					map_temp.put(baseKey, newTaskTimeTemp + "_" + retentTaskTimeTemp + "_" + ttDto.getBaseName());
				} else {
					map_temp.put(baseKey, newTaskTime + "_" + retentTaskTime + "_" + ttDto.getBaseName() );
				}
				
				newTasktimeTotal += newTaskTime;
				retentTaskTimeTotal += retentTaskTime;
			}
		} catch (Exception e) {
			LOGGER.error("查询出错！", e);
		}
		
		DecimalFormat df = new DecimalFormat("#.00");
		List<TasktimeDto> ttDtos = new ArrayList<TasktimeDto>();
		for (Map.Entry<String, String> entry : map_temp.entrySet()) {
             String[] key  = entry.getKey().split("_");
             String[] value  = entry.getValue().split("_");
             
             int newTaskTime =  Integer.valueOf(value[0]);
             int retentTaskTime =  Integer.valueOf(value[1]);
             
             TasktimeDto ttDto = new TasktimeDto();
             ttDto.setBase(Integer.valueOf(key[1]));
             ttDto.setBaseName(value[2]);
             ttDto.setNewTaskTime(newTaskTime);
             ttDto.setRetentTaskTime(retentTaskTime);
             if (newTaskTime == 0 || newTasktimeTotal == 0) {
            	 ttDto.setNewTaskTimeRate(0f);
             } else {
            	 ttDto.setNewTaskTimeRate(BigDecimalUtil.divideToIntegralValue(newTaskTime, newTasktimeTotal));
             }
             if (retentTaskTime == 0 || retentTaskTimeTotal == 0) {
            	 ttDto.setNewTaskTimeRate(0f);
             } else {
            	 ttDto.setRetentTaskTimeRate(BigDecimalUtil.divideToIntegralValue(retentTaskTime, retentTaskTimeTotal));
             }
             
             ttDtos.add(ttDto);
		}
		
		map.put("rows", ttDtos);
		map.put("total", ttDtos.size());
		return map;
	}

}
